/*
 * Copyright (c) 2020-2021, Bluetrum Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020/12/10     greedyhao    The first version
 */

/**
 * Notice!
 * All functions or data that are called during an interrupt need to be in RAM.
 * You can do it the way exception_isr() does.
 */

#include <rtthread.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "board.h"
#include "rgb.h"
#include "adc.h"
#include "pwm.h"
#include "ir.h"
#include "key.h"
#include <drivers/pin.h>
#include "uart.h"
#include "iic.h"

extern  rt_flag_t power;
extern rt_uint8_t add_gear_adj;
extern rt_uint8_t subb_gear_adj;
extern rt_uint8_t pwm_data;


static int Thread_start(void)
{

     rt_thread_t rgb_thread= RT_NULL;  //线程名称
     rt_thread_t adc_thread = RT_NULL;
       rt_thread_t pwm_thread = RT_NULL;
     rt_thread_t key_thread = RT_NULL ;
//  rgb_thread = rt_thread_create("rgb", rgb_thread_entry, RT_NULL, 512, 10, 10);
  adc_thread = rt_thread_create("adc", adc_measure, RT_NULL, 512, 10, 10);
//  pwm_thread =  rt_thread_create("pwm",PWM_Thread_Entry, RT_NULL, 512, 5,5);
  key_thread =  rt_thread_create("key",key_thread_entry, RT_NULL, 512,20,10);
 if(adc_thread == RT_NULL)
 {
 rt_kprintf("Thread_ADC Init ERROR");
 return RT_ERROR;
 }
//  rt_thread_startup(rgb_thread);
   rt_thread_startup(adc_thread);
 // rt_thread_startup(pwm_thread);
  rt_thread_startup(key_thread);
  thread_serial();
}
INIT_APP_EXPORT(Thread_start);




int main(void)
{
    _irrx_hw_init();
     Pwm_Init();
     rt_pin_mode(rt_pin_get("PA.5"), PIN_MODE_OUTPUT);//风扇
     TM1680_logo();
    while (1)
    {
        if(power)
            {
            rt_pin_write(rt_pin_get("PA.5"),PIN_HIGH);
            }
        else
            {
            rt_pin_write(rt_pin_get("PA.5"),PIN_LOW);
           if(pwm_data!=pwm_data) pwm_vout_adj(35,0);
            }
         ir_receive();
         rt_thread_mdelay(100);


    }
}

